<?php

class PhotoModel extends DBModel
{
	protected $_table = '{{photo}}';
	protected $_id = 'photoid';
	
	public function createPhoto($file_name, $isview = 0)
	{
		/*
		$upload_file = PHOTO_DIR . '/' . $file_name;
		Yii::import('ext.file.ImageHelper');
		$largepath = 'large_' . $file_name;
		$config = array('width' => '200', 'height' => '200');
		ImageHelper::getInstance($upload_file)->resize(PHOTO_DIR . '/' . $largepath, $config);
			
		$midpath = 'mid_' . $file_name;
		$config = array('width' => '100', 'height' => '100');
		ImageHelper::getInstance($upload_file)->resize(PHOTO_DIR . '/' . $midpath, $config);
			
		$smallpath = 'small_' . $file_name;
		$config = array('width' => '50', 'height' => '50');
		ImageHelper::getInstance($upload_file)->resize(PHOTO_DIR . '/' . $smallpath, $config);
		*/
		
		$upload_file = PHOTO_DIR . '/' . $file_name;
		Yii::import('ext.file.ImageHelper');
		$largepath = 'large_' . $file_name;
		$config = array('width' => '1920');
		ImageHelper::getInstance($upload_file)->resize(PHOTO_DIR . '/' . $largepath, $config, 'width');
			
		$midpath = 'mid_' . $file_name;
		$config = array('width' => '1200');
		ImageHelper::getInstance($upload_file)->resize(PHOTO_DIR . '/' . $midpath, $config, 'width');
			
		$smallpath = 'small_' . $file_name;
		$config = array('width' => '360', 'height' => '360');
		ImageHelper::getInstance($upload_file)->resize_to_crop(PHOTO_DIR . '/' . $smallpath, $config, array('resize' => 'width'));
			
		//生成缩略图
		$photo_data = array(
			'path' => $file_name,
			'largepath' => $largepath,
			'midpath' => $midpath,
			'smallpath' => $smallpath,
			'createtime' => time(),
			'isview' => $isview
		);
		$this->insert($photo_data);
		return $this->getLastInsertID();
	}
	
	public function delByPhotoID($photoid)
	{
		return $this->delete('photoid = :photoid', array(':photoid' => $photoid));
	}
	
	public function getListIn($photoid)
	{
		if ($photoid == '') {
			return false;
		}
		$where['condition'] = 'photoid IN ('. $photoid .')';
		return $this->findAll($where);
	}
}